package com.huan.log;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class LogMapper extends Mapper<LongWritable, Text,Text, NullWritable> {

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //TODO 获取一行
        String line = value.toString();

        //TODO 解析数据
        boolean result = parseLog(line,context);

        if (!result){
            return;
        }
        //TODO 解析通过，写出去
        context.write( value,NullWritable.get() );

    }

    private boolean parseLog(String line, Context context) {

        String[] fields = line.split( " " );

        if (fields.length > 11 ){
            context.getCounter( "map","true" );
            return true;
        }else {
            context.getCounter( "map","false" );
            return false;
        }

    }
}
